package dhz.tools.popup.actions;

import org.apache.log4j.Logger;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import dhz.tools.service.OpenDirService;

/**
 * 打开资源目录或所在目录
 * @author dhz
 */
public class OpenDirAction implements IObjectActionDelegate {

	private static final Logger LOG = Logger.getLogger(OpenDirAction.class);
	private Shell shell;
	private ISelection selection;
	
	public OpenDirAction() {
		super();
	}

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	public void run(IAction action) {
		StructuredSelection sel = (StructuredSelection)selection;
		IResource resource = (IResource)sel.getFirstElement();
		try {
			new OpenDirService().openDir(resource.getLocation().toFile());
		} catch (Exception e) {
			LOG.error("打开目录时出错",e);
			MessageDialog.openError(shell,"错误",e.getMessage());
		}
	}

	public void selectionChanged(IAction action, ISelection selection) {
		this.selection  = selection;
	}

}
